package net.ufrog.common.xls.disposer;

import net.ufrog.common.exception.ServiceException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;

/**
 * 字符串处理器
 * 
 * @author ultrafrog
 * @version 1.0, 2015-08-30
 * @since 1.0
 */
public class StringDisposer extends Disposer {

    @Override
    public Object get(Cell cell, net.ufrog.common.xls.annotation.Cell annotation) {
        if (cell.getCellTypeEnum() == CellType.STRING) {
            return cell.getStringCellValue();
        } else if (cell.getCellTypeEnum() == CellType.NUMERIC) {
            return cell.getNumericCellValue() + "";
        } else if (cell.getCellTypeEnum() == CellType.BLANK) {
            return null;
        } else {
            throw new ServiceException("cannot parse cell '" + cell.getCellTypeEnum() + "' value to String.");
        }
    }

    @Override
    public void set(Cell cell, Object value, net.ufrog.common.xls.annotation.Cell annotation) {
        cell.setCellType(CellType.STRING);
        cell.setCellValue(String.valueOf(value));
    }
}
